<?php

/**
 * promotions actions.
 *
 * @package    retailbrain
 * @subpackage promotions
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class promotionsActions extends sfActions {

    /**
     * Termina la session por inactividad
     */
    public function preExecute() {
        if (!$this->getUser()->isAuthenticated()) {
            $this->getUser()->setAuthenticated(false);
            $this->getUser()->getAttributeHolder()->clear();
            $this->redirect('/login');
        }
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->title = "Promociones";
        $this->breadcrumbs = array(array("Promociones", "promotions/index"));

        $user = $this->getUser()->getAttribute('id_users');
        $this->caps = $this->getUser()->getAttribute('caps');


        $q = Doctrine_Query::create()
                ->from('UserTable ut')
                ->leftJoin('ut.UserTableOrder uto')
                ->where('ut.table_user = ?', array('Promociones'))
                ->addWhere('uto.id_users = ?', array($this->getUser()->getAttribute('id_users')))
                ->orderBy('uto.orden');

        $this->userTableOrder = $q->fetchArray();

        $this->userOrderCount = count($q->fetchArray());


        if (!$this->userOrderCount) {
            $q = Doctrine_Query::create()
                    ->from('UserTable ut')
                    ->leftJoin('ut.UserTableOrder uto')
                    ->where('ut.table_user = ?', array('Promociones'))
                    ->orderBy('ut.id_tablecolumn');

            $this->userTableOrder = $q->fetchArray();
        }
        
        
        $id = $request->getParameter('id');
         
        /** Promotions * */
        $q = Doctrine_Query::create()
                ->from('StorePromotions sp')
                ->innerJoin('sp.Products p')
                ->innerJoin('sp.CategoryProduct cp')
                ->innerJoin('sp.Brands b')
                ->innerJoin('sp.ProductsFormats pf')
                ->where('sp.id_store = ?', array($id))
                ->orderBy('p.name');

        $this->promotions = $q->fetchArray();
        
        
        
        
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeCreateOrder(sfWebRequest $request) {


        $order = $request->getParameter('order');
        $visible = $request->getParameter('visible');
        $table_column = $request->getParameter('tableColumn');

        $user = $this->getUser()->getAttribute('id_users');

        myFunctionsTables::setOrderTable($table_column, $order, $visible, $user, "Promociones");

        $this->redirect('/promotions/index');
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeClearOrder(sfWebRequest $request) {

        $this->setLayout(false);
        $user = $this->getUser()->getAttribute('id_users');

        $this->forward404Unless($order = Doctrine::getTable('UserTableOrder')->findBySql("id_users = ? and table_user = ?", array($user, "Promociones")));
        try {
            $order->delete();
        } catch (Exception $e) {
            //$this->redirect('cliente/index');
        }

        $this->redirect('/promotions/index');
    }

    public function executeExcelPromotions(sfWebRequest $request) {

        $objPHPExcel = new PHPExcel();

        // Set properties
        $objPHPExcel->getProperties()->setCreator("RetailBrain");
        $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
        $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
        $objPHPExcel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
        $objPHPExcel->getActiveSheet()->getPageSetup()->setFitToHeight(0);

        // Add some data
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 1, 'CADENA');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 1, 'PRODUCTO');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, 1, 'PRECIO PROMOCION');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, 1, 'FORMATO PROMOCION');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, 1, 'DESCUENTO');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, 1, 'CÓDIGO');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, 1, 'CATEGORÍA');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, 1, 'MARCA');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, 1, 'ÚNIDADES');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, 1, 'CATALOGO');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, 1, 'PRECIO SUGERIDO');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, 1, 'FECHA INICIO');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, 1, 'FECHA TERMINO');


        $id = $request->getParameter('id');
         /** Promotions * */
        $q = Doctrine_Query::create()
                ->from('StorePromotions sp')
                ->innerJoin('sp.Products p')
                ->innerJoin('sp.CategoryProduct cp')
                ->innerJoin('sp.Brands b')
                ->innerJoin('sp.ProductsFormats pf')
                ->where('sp.id_store = ?', array($id))
                ->orderBy('p.name');

        $promotions = $q->fetchArray();
        
         $i = 2;
        foreach ($promotions as $value) {

            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, " ");
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $value['Products']['name']);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $value['price_promotion']);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $value['ProductsFormats']['name']);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $value['discount']);
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $value['code_customer']);
            $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $value['CategoryProduct']['name']);
            $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $value['Brands']['name']);
            $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $value['units']);
            $objPHPExcel->getActiveSheet()->setCellValue('J' . $i, $value['catalog']);
            $objPHPExcel->getActiveSheet()->setCellValue('K' . $i, $value['price_suggested']);
            $objPHPExcel->getActiveSheet()->setCellValue('L' . $i, $value['promotion_date_start']);
            $objPHPExcel->getActiveSheet()->setCellValue('M' . $i, $value['promotion_date_end']);

            $i++;
        }

                        
        $user = $this->getUser()->getAttribute('id_users');

        $objPHPExcel->getActiveSheet()->setTitle('Promociones');

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="promociones.xlsx"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        exit;

        return sfview::NONE;
    }

}
